package com.shinektv.lrclib;

import static com.shinektv.lrclib.LrcTextView.STROKE_WIDTH;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 带描边的歌词字幕
 */
public class StrokeView extends RelativeLayout {
    private Context context;
    //歌词文本
    private TextView textView;
    //描边
    private TextView strokeTextView;

    public StrokeView(Context context) {
        this(context, null);
        init(context);
    }

    public StrokeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        init(context);
    }

    public StrokeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    //蓝字白边
    private void init(Context context) {
        this.context = context;

        AssetManager manager = context.getAssets();
        String path = "fonts/Roboto-Bold.ttf";
        Typeface type = Typeface.createFromAsset(manager, path);

        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        textView = new TextView(context);
        textView.setTypeface(type);
        textView.setLayoutParams(params);
        strokeTextView = new TextView(context);
        strokeTextView.setLayoutParams(params);
        strokeTextView.setTypeface(type);

        textView.setTextSize(60);
        textView.setEllipsize(null);
        textView.setSingleLine();
        TextPaint paint1 = textView.getPaint();
        paint1.setStrokeWidth(3);
        paint1.setStyle(Paint.Style.FILL_AND_STROKE);

        strokeTextView.setTextSize(60);
        strokeTextView.setEllipsize(null);
        strokeTextView.setSingleLine();

        TextPaint paint = strokeTextView.getPaint();
        paint.setStrokeWidth(STROKE_WIDTH);  //描边宽度
        paint.setStyle(Paint.Style.STROKE);
//        paint.setShadowLayer(LrcTextView.SHADOW_LAYER_RADIUS, 0, 0, Color.WHITE);//在四周加阴影
//        paint.setFakeBoldText(true);//加粗，效果不是很明显
        addView(textView);
        addView(strokeTextView);
    }

    public void setLrc(String lrc) {
        textView.setText(lrc);
        strokeTextView.setText(lrc);
    }

    //设置字号
    public void setTextSize(int size) {
        textView.setTextSize(size);
        strokeTextView.setTextSize(size);
    }

    //字体加粗
    public void setTextBlod(int strokeWidth) {
        textView.getPaint().setStrokeWidth(strokeWidth);
    }

    //设置歌词颜色
    public void setTextColor(int color) {
        textView.setTextColor(color);
    }

    //设置描边颜色
    public void setStrokeColor(int color) {
        strokeTextView.setTextColor(color);
    }

    //设置描边粗细
    public void setStrokeWidth(int width) {
        strokeTextView.getPaint().setStrokeWidth(width);
    }

    //设置阴影
    public void setShadowLayerColor(float radius, int color) {
        strokeTextView.getPaint().setShadowLayer(radius, 0, 0, color);
    }

    //设置宽度
    public void setWidth(int width) {
        textView.setWidth(width);
        strokeTextView.setWidth(width);
    }
}
